package com.pulaien.web.controller.fundmanagement;

import com.alibaba.fastjson.JSONObject;
import com.pulaien.common.annotation.Anonymous;
import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.domain.R;
import com.pulaien.common.core.page.TableDataInfo;
import com.pulaien.common.enums.CapitalEnum;
import com.pulaien.common.utils.SecurityUtils;
import com.pulaien.common.utils.validator.ValidationGroups;
import com.pulaien.system.domain.dto.admin.MallWithdraw;
import com.pulaien.system.domain.dto.admin.MallWithdrawParam;
import com.pulaien.system.service.fundmanagement.WithdrawService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liangjiagang
 * @title: WithdrawController
 * @projectName mall
 * @description: 提现
 * @date 2021/11/914:35
 */
@Validated
@RestController
@Api(tags = "买家端-账户提现", description = "买家端-账户提现")
@RequestMapping("/shop/buyer/Withdraw")
public class BuyerWithdrawController extends BaseController {

    private final WithdrawService withdrawService;

    public BuyerWithdrawController(WithdrawService withdrawService) {
        this.withdrawService = withdrawService;
    }


    @ApiOperation(value = "新增提现")
    @PostMapping("/addWithdraw")
    @Transactional
    public R<JSONObject> addWithdraw(
            @RequestBody @Validated(ValidationGroups.WithdrawInsert.class) MallWithdrawParam param
    ) {
        return withdrawService.addWithdraw(param);
    }


    @ApiOperation(value = "分页查询提现信息")
    @GetMapping(value = "/findWithdraw")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", defaultValue = "1", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", defaultValue = "10", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "query"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向 \"desc\" 或者 \"asc\"", paramType = "query"),
            @ApiImplicitParam(name = "reasonable", value = "分页参数合理化", paramType = "query", dataType = "Boolean")
    })
    public TableDataInfo findWithdraw(
            MallWithdrawParam param
    ) {
        startPage();
        param.setUserId(SecurityUtils.getUserId());
        List<MallWithdraw> record = withdrawService.findWithdraw(param);
        return getDataTable(record);
    }

    @ApiOperation(value = "取消提现")
    @GetMapping(value = "/cancelWithdraw/{id}")
    @Transactional
    public TableDataInfo cancelRecharge(
            @PathVariable("id") Long id
    ) {
        MallWithdrawParam update = new MallWithdrawParam();
        update.setId(id);
        update.setWithdrawStatus(CapitalEnum.WITHDRAW_STATUS2.getCode());
        withdrawService.cancelRecharge(update);

        MallWithdrawParam query = new MallWithdrawParam();
        query.setId(id);
        startPage();
        List<MallWithdraw> record = withdrawService.findWithdraw(query);
        return getDataTable(record);
    }
}
